package com.dex4d.ui.main.socket;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.TextView;

import com.dex4d.core.CustomApplication;
import com.dex4d.model.User;
import com.dex4d.ui.main.model.AccountModel;
import com.dex4d.ui.main.model.CoinToken;
import com.dex4d.ui.main.model.TokenDetail;

import java.util.List;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 作者    LiQingyi
 * 时间    2018/11/7 14:41
 * 文件    dex4d-android
 * 描述
 */
public class Receipt {

    private Subscription subscription;
    private RPCRequest rpcRequest;
    private int pendingTotal;

    public Receipt() {
        rpcRequest = new RPCRequest();
        rpcRequest.id = "1";
        rpcRequest.method = "kline_subscribe";
        rpcRequest.tag = "personTradingList";
        rpcRequest.params.add(rpcRequest.tag);
        rpcRequest.params.add(User.getInstance().getAddress());
    }

    /**
     * 开始
     */
    public void start() {
        subscription = Observable
                .interval(0, 5, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        send();
                    }
                });
    }

    /**
     * 停止订阅
     */
    public void stop() {
        if (subscription != null && !subscription.isUnsubscribed()) {
            subscription.unsubscribe();
        }

    }

    /**
     * 发送消息
     */
    public void send() {
        CustomApplication.getClient().sendRpcRequest(rpcRequest);
    }

    private String getPendingTotal() {
        if (this.pendingTotal < 1) {
            return "";
        } else {
            return String.format("(%1$s Trade Pending)", this.pendingTotal);
        }
    }

    public void updatePendingTotal() {
        this.pendingTotal++;
    }

    /**
     * 监听事件更新UI
     *
     * @param rpc
     * @param symbol
     * @param label
     */
    public void updateUI(RPC rpc, String symbol, TextView label) {
        if (rpc != null && rpc.params != null) {
            if (TextUtils.equals(rpc.tag, rpcRequest.tag)) {
                RPCParamList rpcParam = rpc.params;
                if (rpcParam.result != null && !rpcParam.result.isEmpty()) {
                    List<TokenDetail> list = rpcParam.result;
                    for (TokenDetail detail : list) {
                        if (TextUtils.equals(detail.Tokens, String.format("Dex4D/%1$s", symbol))) {
                            if (TextUtils.equals("Pending", detail.status)) {
                                updatePendingTotal();
                            }


                        }
                    }
                    //更新UI
                    label.setText(getPendingTotal());
                }
            }
        }
    }

    /**
     * 更新列表
     *
     * @param rpc
     */
    public void updateListUI(RPC rpc, List<AccountModel> modelList, RecyclerView.Adapter adapter) {

        if (modelList == null) {
            return;
        }

        if (rpc != null && rpc.params != null) {
            if (TextUtils.equals(rpc.tag, rpcRequest.tag)) {
                RPCParamList rpcParam = rpc.params;
                if (rpcParam.result != null && !rpcParam.result.isEmpty()) {
                    List<TokenDetail> list = rpcParam.result;
                    for (AccountModel model : modelList) {
                        CoinToken token = model.coinToken;
                        token.pending = 0;
                        for (TokenDetail detail : list) {
                            if (TextUtils.equals(detail.Tokens, String.format("Dex4D/%1$s", token.symbol))) {
                                if (TextUtils.equals("Pending", detail.status)) {
                                    token.pending++;
                                }
                            }
                        }
                    }

                    //更新UI
                    adapter.notifyDataSetChanged();
                }
            }
        }
    }


}
